/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pos.Driving.Task;

import java.util.*;
import pos.Driving.Train.Train;
import pos.IModeController;
import pos.utils.*;

/**
 *
 * @author plus
 */
public class TaskPlayer implements Invokeable{
    private static final int Task_Playing=1, Task_Paused=0;
    private final boolean DEBUG=true;
    private IModeController myController;
    private ArrayList<Task> taskTable;                        //任务表
    private int currentTaskID=0;
    private Task currentTask;
    private HashMap<String, Double> subPoints;       //子项目得分记录，用于Task之间的数据交换
    private Double [] playerCounter;                             //任务计时,供Task使用，单位ms
    private int currentStatue;
    
    public TaskPlayer(IModeController myController) {
        this.myController = myController;
        taskTable = new ArrayList<Task> ();
        subPoints = new HashMap<String, Double>();
        playerCounter=new Double[]{new Double(0)};
        currentStatue=Task_Paused;
    }

    public void addTask(Task task){
        task.myController= this.myController;               //公用变量引用注入
        task.subPoints=this.subPoints;
        task.playerCounter=playerCounter;
        taskTable.add(task);
    }

    public void play(){
        this.currentStatue=Task_Playing;
    }
    public void pause(){
        this.currentStatue=Task_Paused;
    }
    public void reset(){
        currentTaskID=0;
        currentTask=null;
        subPoints = new HashMap<String, Double>();
        playerCounter[0]=0d;
    }

    @Override
    public void invoke(double Step) {
        if (this.currentStatue==Task_Playing){
            playTask();
            playerCounter[0]+=Step;
            ((Train)myController.doEvent("GetTrain", null)).
                    getDashboard().setTime(playerCounter[0]);
        }
    }

    private void playTask(){
        if (currentTask==null){
             if (currentTaskID==0 && taskTable.size()>0){   //Start the first task
                currentTaskID=1;
                currentTask=taskTable.get(currentTaskID-1);
                currentTask.initProcess();
                if (DEBUG){
                    System.out.println(currentTask.getTaskName()+"Started!");
                }
             }else
                 pause();
        }else{
            int conditionCode=currentTask.isReachConditions();
            if (conditionCode > 0){
                currentTask.postProcess(conditionCode);
                if (DEBUG){
                    System.out.println(currentTask.getTaskName()
                            +"Finished with code "+conditionCode);
                }
                currentTaskID++;
                if (currentTaskID <= taskTable.size()){
                    currentTask=taskTable.get(currentTaskID-1);
                    currentTask.initProcess();
                    if (DEBUG){
                        System.out.println(currentTask.getTaskName()+"Started!");
                    }
                }else
                    pause();
            }
        }


    }
}
